package com.bs.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.bs.bean.Item;
import com.bs.dao.ItemDAO;

public class ItemDAOImpl extends SqlMapClientDaoSupport implements ItemDAO {

	@Override
	public int delete(Item item) {
		return this.getSqlMapClientTemplate().delete("item.delete",item);
	}
	
	
	
	@Override
	public int batchDelete(String ids) {
		return this.getSqlMapClientTemplate().delete("item.batchDelete",ids);
	}



	@SuppressWarnings("unchecked")
	@Override
	public List<Item> findAll(int start, int limit) {
		Map<String,Integer> map = new HashMap<String,Integer>();
		map.put("start", start);
		map.put("limit", limit);
		return this.getSqlMapClientTemplate().queryForList("item.findAllForPage",map);
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public List<Item> findAll(String start, String end, String type) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("start", start);
		map.put("end", end);
		map.put("type",type);
		return this.getSqlMapClientTemplate().queryForList("item.findAllBySeason",map);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Item> findAll() {
		return this.getSqlMapClientTemplate().queryForList("item.findAllForGroup");
	}
	
	@Override
	public void save(Item item) throws DataAccessException {
		this.getSqlMapClientTemplate().insert("item.save", item);
	}

	@Override
	public int update(Item item, String type) {
		if(type.equals("sell")){
			return this.getSqlMapClientTemplate().update("item.updateForSell",item);
		}else if(type.equals("back")){
			return this.getSqlMapClientTemplate().update("item.updateForBack",item);
		}
		return -1;
	}
	@Override
	public int totalCount() {
		return (Integer)this.getSqlMapClientTemplate().queryForObject("item.totalCount");
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Item> sumBySeason(String start, String end, String type) {
		List<Item> list = new ArrayList<Item>();
		Map<String,String> map = new HashMap<String,String>();
		map.put("start", start);
		map.put("end", end);
		map.put("type", type);
		list = this.getSqlMapClientTemplate().queryForList("item.sumBySeason",map);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Item> busiBonusBySeason(String start, String end, String type) {
		List<Item> list = new ArrayList<Item>();
		Map<String,String> map = new HashMap<String,String>();
		map.put("start", start);
		map.put("end", end);
		map.put("type", type);
		list = this.getSqlMapClientTemplate().queryForList("item.busiBonusBySeason",map);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Item> supportBonusBySeason(String start, String end, String type) {
		List<Item> list = new ArrayList<Item>();
		Map<String,String> map = new HashMap<String,String>();
		map.put("start", start);
		map.put("end", end);
		map.put("type", type);
		list = this.getSqlMapClientTemplate().queryForList("item.supportBonusBySeason",map);
		return list;
	}



	@SuppressWarnings("unchecked")
	@Override
	public List<Item> managerBonusBySeason(String start, String end, String type) {
		List<Item> list = new ArrayList<Item>();
		Map<String,String> map = new HashMap<String,String>();
		map.put("start", start);
		map.put("end", end);
		map.put("type", type);
		list = this.getSqlMapClientTemplate().queryForList("item.managerBonusBySeason",map);
		return list;
	}



	@SuppressWarnings("unchecked")
	@Override
	public List<Item> teamBonusBySeason(String start, String end, String type) {
		List<Item> list = new ArrayList<Item>();
		Map<String,String> map = new HashMap<String,String>();
		map.put("start", start);
		map.put("end", end);
		map.put("type", type);
		list = this.getSqlMapClientTemplate().queryForList("item.teamBonusBySeason",map);
		return list;
	}



	@SuppressWarnings("unchecked")
	@Override
	public Item sumByPerson(String start, String end, String type, int personId) {
		@SuppressWarnings("rawtypes")
		Map map = new HashMap();
		map.put("start", start);
		map.put("end", end);
		map.put("type", type);
		map.put("personId", personId);
		return (Item) this.getSqlMapClientTemplate().queryForObject("item.sumByBusi",map);
	}



	@Override
	public Item sumBySupportType(String start, String end, String type, String supportType) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("start", start);
		map.put("end", end);
		map.put("type", type);
		map.put("productType", supportType);
		return (Item) this.getSqlMapClientTemplate().queryForObject("item.sumBySupportType",map);
	}



	@SuppressWarnings("unchecked")
	@Override
	public Item sumByManager(String start, String end, String type, int personId) {
		@SuppressWarnings("rawtypes")
		Map map = new HashMap();
		map.put("start", start);
		map.put("end", end);
		map.put("type", type);
		map.put("personId", personId);
		return (Item) this.getSqlMapClientTemplate().queryForObject("item.sumByManager",map);
	}



	@SuppressWarnings("unchecked")
	@Override
	public List<Item> query(Item item) {
		// TODO Auto-generated method stub
		return (List<Item>) this.getSqlMapClientTemplate().queryForList("item.query",item);
	}
	
	
	
	
}
